package com.yunc.base.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.yunc.base.oss.AliyunCloudStorageService;
import com.yunc.base.properties.Pro;
import com.yunc.base.utils.BaseUtil;
import com.yunc.base.utils.R;
import lombok.RequiredArgsConstructor;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 文件上传
 * 1、直接保存
 * 2、单独完成一个服务器用于保管图片,(1)可以使用java,(2)分布式文件存储FastDFS
 * 3、使用阿里云
 *
 * @author taocongcong
 * @create 2018-06-08 15:51
 */
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class FileController {

    private final Pro pro;
    private final AliyunCloudStorageService aliyunCloudStorageService;

    @RequestMapping(value="/aliyun/fileUpload")
    public R fileUpload(@RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException {
        // 这里实现文件上传操作用的是commons.io.FileUtils类,它会自动判断路径是否存在,不存在会自动创建
        // String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/classes/static/upload");
        // 上传文件的原名(即上传前的文件名字)
        // String newFileName = null;
        List<String> urlList = new ArrayList<>();
        for (MultipartFile myfile : myfiles) {
            if (myfile.isEmpty()) {
                return null;
            } else {
                // byte[] bytes = ThumbnailsUtil.autoSize(myfile.getInputStream(), 120, 80);
                String newFileName = BaseUtil.getFileNameByDate(myfile.getOriginalFilename());
                String url = aliyunCloudStorageService.upload(myfile.getInputStream(), newFileName);
                urlList.add(url);
            }
        }
        return R.ok().put("errno", 0).put("data", urlList);
    }

    @RequestMapping(value="/fileUpload")
    public R aliyunFileUpload(@RequestParam MultipartFile[] myfiles, HttpServletRequest request) throws IOException {
        // 这里实现文件上传操作用的是commons.io.FileUtils类,它会自动判断路径是否存在,不存在会自动创建
        String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/classes/static/upload");
        // 上传文件的原名(即上传前的文件名字)
        // String newFileName = null;
        List<String> urlList = new ArrayList<>();
        for (MultipartFile myfile : myfiles) {
            if (myfile.isEmpty()) {
                return null;
            } else {
                // byte[] bytes = ThumbnailsUtil.autoSize(myfile.getInputStream(), 120, 80);
                String newFileName = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss") +
                        BaseUtil.acceptRandom(BaseUtil.NUMBER_CONTENT, 6) + "." +
                        BaseUtil.acceptFileType(myfile.getOriginalFilename());
                // 本地保存
                try {
                    FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, newFileName));
                    String url = pro.getBaseVisitURL() + "upload/" + newFileName;
                    urlList.add(url);
                } catch (IOException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        }
        return R.ok().put("errno", 0).put("data", urlList);
    }

    @RequestMapping(value = "/qrcode/shareQrCode", produces = MediaType.IMAGE_JPEG_VALUE)
    public byte[] shareQrCode(String url){
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.CYAN.getRGB());
        // 设置背景色（灰色）
        config.setBackColor(Color.GRAY.getRGB());
        config.setQrVersion(1);
        return QrCodeUtil.generatePng(url, config);
    }
}
